<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .grandfather{
            position: relative;
            margin: 100px;
            width: 250px;
            height: 250px;
            background-color:brown;
        }
        .father{
            margin-left: 100px;
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
        .son{
            margin-left: 45px;
            width: 50px;
            height: 50px;
            background-color:blueviolet;
        }
        .w{
            width: 100px;
            height: 100px;
            margin: 300px;
            background-color: cadetblue;
            padding: 50px;
            border: 15px solid black;
        }
    </style>
</head>
<body>
    <div class="grandfather">
        <div class="father">
            <div class="son">
    
            </div>
        </div>
    </div>
    <div class="w">

    </div>
    <script>
        var father = document.querySelector(".father")
        var son = document.querySelector(".son")
        var w = document.querySelector(".w")
        console.log(father.offsetLeft)
        console.log(father.offsetTop)
        //offsetTop,offsetLeft属性为相对具有定位布局的父元素取得偏移量 如果父元素没有定位布局 则子元素会一直往上查找直到body为止
        console.log(son.offsetLeft)
        console.log(son.offsetTop)

        //offsetWidth,offsetHeight属性 会返回元素的大小 但是返回值为 width/height+padding + 边框之和,
        console.log(w.offsetWidth)
        console.log(w.offsetHeight)
    </script>
</body>
</html>